package com.haha.email.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Properties;

/**
 * description: 加载相关配置文件  email account , password , server info .
 * @version v1.0
 * @author w
 * @date 2019年7月30日上午11:01:09
 **/
public class ResourcesLoader {
	@SuppressWarnings("unused")
	private ResourcesLoader resourcesLoader = new ResourcesLoader();
	
	/**
	 * 创建 PROPERTIES对象，存储加载的配置文件信息
	 */
	private static final Properties PROPERTIES = new Properties();
	
	public static void loader(String ...path) {
		if(path ==null || path.length == 0) {
			throw new IllegalArgumentException("property path can't be blank");
		}
		for (String p : path) {
			if(!p.startsWith("/")) {
				p = "/"+p;
			}
			InputStream resourceAsStream = ResourcesLoader.class.getResourceAsStream(p);
			InputStreamReader reader = null ;
			try {
				reader = new InputStreamReader(resourceAsStream , "UTF-8");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			try {
				PROPERTIES.load(reader);
				resourceAsStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static Properties getProperties() {
		return PROPERTIES ;
	}
	
	/**
	 * description: 获取相关属性配置 
	 * @param key
	 * @return String
	 * @version v1.0
	 * @author w
	 * @date 2019年7月30日 上午11:09:41
	 */
	public String getConfig(String key) {
		if(key == null) {
			return "";
		}
		return PROPERTIES.getProperty(key);
	}
	
	public static void main(String[] args) {
		ResourcesLoader.loader("email.properties");
		System.out.println(getProperties());
		URL resource = Thread.currentThread().getContextClassLoader().getResource("email.properties");
		System.out.println(resource.getFile());
	}	
	
}
